/*LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL
【平    台】龙邱i.MX RT1064核心板-智能车板
【编    写】LQ-005
【E-mail  】chiusir@163.com
【软件版本】V1.0
【最后更新】2019年3月13日
【相关信息参考下列地址】
【网    站】http://www.lqist.cn
【淘宝店铺】http://shop36265907.taobao.com
------------------------------------------------
【dev.env.】IAR8.30.1及以上版本
QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ*/
#include "LQ_GPIO_Cfg.h"
#include "ICM20602_SOFTSPI.h"
#include "fsl_common.h"
#include "fsl_iomuxc.h"
#include "pin_mux.h"
#include "fsl_gpio.h"
#include "LQ_GPIO.h"
/**
  * @brief    模拟SPI 延时函数
  *
  * @param    
  *
  * @return   
  *
  * @note     
  *
  * @example  
  *
  * @date     2019/5/28 星期二
  */
void SPI_SoftDelay(void)
{
    __asm("NOP");
    __asm("NOP");
    __asm("NOP");
}


/**
  * @brief    模拟SPI初始化
  *
  * @param    
  *
  * @return   
  *
  * @note     
  *
  * @example  
  *
  * @date     2019/5/28 星期二
  */
void SPI_SoftInit(void)
{
    PIN_InitConfig(SOFT_SPI_CS_PIN, PIN_MODE_OUTPUT, 1);
    PIN_InitConfig(SOFT_SPI_SCK_PIN, PIN_MODE_OUTPUT, 1);
    PIN_InitConfig(SOFT_SPI_MISO_PIN, PIN_MODE_INPUT_PULLDOWN, 0);
    PIN_InitConfig(SOFT_SPI_MOSI_PIN, PIN_MODE_OUTPUT, 1);
    
    // IOMUXC_SetPinMux(IOMUXC_GPIO_EMC_20_GPIO4_IO20,1U);   //管脚MISO
    IOMUXC_SetPinMux(IOMUXC_GPIO_EMC_28_GPIO4_IO28,1U);      
    SPI_SCK_IDLE;
}

/**
  * @brief    模拟SPI读写n 字节
  *
  * @param    buff[] 存放要写入的值 写入后存放读取的值
  *
  * @return   
  *
  * @note     
  *
  * @example  
  *
  * @date     2019/5/28 星期二
  */
void SPI_SoftRadeWriteNbyte(uint8_t *buff, uint16_t len)
{
    SPI_CS_OUT(0);
    SPI_SCK_IDLE;
//    SPI_SoftDelay();
#if CPHA    //第二个时钟跳边沿
    do
    {
        for(uint8_t i = 0; i < 8; i++)
        {
            SPI_MOSI_OUT((*buff) >= 0x80);
            SPI_SCK_ACTIVE;
            (*buff) = (*buff)<<1;
            
            SPI_SoftDelay();
            SPI_SCK_IDLE;
            SPI_SoftDelay();
            (*buff) |= SPI_MISO_IN;          
        }
        buff++;
    }while(--len);
#else
    do
    {
        for(uint8_t i = 0; i < 8; i++)
        {

            SPI_MOSI_OUT((*buff) >= 0x80);
            (*buff) = (*buff)<<1;
            SPI_SCK_ACTIVE;
            SPI_SoftDelay();
            (*buff) |= SPI_MISO_IN;
            SPI_SCK_IDLE;
            SPI_SoftDelay();           
        }
        buff++;
    }while(--len);
    
    
#endif
    SPI_SCK_IDLE;
    SPI_CS_OUT(1);
    
}
